/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "tabpanel.h"



namespace tri {
namespace ui {


extTabButton::extTabButton():extCheckBox(){}
extTabButton::~extTabButton(){}

//============================================


extTabButtonPanel::extTabButtonPanel() :
	tri::ui::extPanel(),mButtonStyles(""){
}

extTabButtonPanel::~extTabButtonPanel() {
}

void extTabButtonPanel::setActive(int index){
	mButtonGroup.setActive(index);
}

bool extTabButtonPanel::containsTab(std::string tab){
	if (tab == "")
	{
		return false;
	}
	std::map<std::string,extTabButton*>::iterator it;
	for ( it=mButtonMap.begin() ; it != mButtonMap.end(); it++ ){
		if ( (*it).first == tab ){
			return true;
		}
	}
	return false;

}

void extTabButtonPanel::addTabButton(std::string tabName,std::string text){
	if (! containsTab(tabName) && tabName != ""){
		mButtonMap[tabName] = new extTabButton();
		mButtonMap[tabName]->setFitToTextWidth(true);
		mButtonMap[tabName]->setText(text.c_str());
		mButtonMap[tabName]->setStyle(mButtonStyles);
		if(text == ""){
			mButtonMap[tabName]->setText(tabName.c_str());
		}
		addComponent(mButtonMap[tabName]);
		mButtonGroup.addCheckBoxToGroup(mButtonMap[tabName]);
	}
}

extTabButton* extTabButtonPanel::removeButton(std::string tabName){
	if(containsTab(tabName)){
		extTabButton* b = mButtonMap[tabName];
		removeEntity(b);
		mButtonMap.erase(tabName);
		mButtonGroup.removeFromGroup(mButtonGroup.find(b));
		return b;
	}
	
	return NULL;
}

void extTabButtonPanel::getButtonList(std::vector<std::string>& buttonNames){
	buttonNames.clear();
	std::map<std::string,extTabButton*>::iterator it;
	for ( it=mButtonMap.begin() ; it != mButtonMap.end(); it++ ){
		buttonNames.push_back((*it).first);
	}
}

void extTabButtonPanel::update(){
	tri::ui::extPanel::update();
	
	tri::TVector ps = getSize();
	float px = 0;
	
	std::map<std::string,extTabButton*>::iterator it;
	for ( it=mButtonMap.begin() ; it != mButtonMap.end(); it++ ){
		tri::TVector cs = (*it).second->getSize();
		cs.y = ps.y;
		(*it).second->setSize(cs);
		tri::TVector cp = (*it).second->getPosition();
		cp.x = px;
		cp.y = 0;
		(*it).second->setPosition(cp);
		px+=cs.x;
	}
	
}

void extTabButtonPanel::setStyle(std::string style){
	tri::ui::extPanel::setStyle(style);
	mButtonStyles = style +std::string(".TabButton");
	std::map<std::string,extTabButton*>::iterator it;
	for ( it=mButtonMap.begin() ; it != mButtonMap.end(); it++ ){
		(*it).second->setStyle(mButtonStyles);
	}
}

extTabButton* extTabButtonPanel::getSelected(){
	return (extTabButton*)mButtonGroup.getSelected();
}

//=========================================

extTabPanel::extTabPanel()
	: extPanel() {
	
	addComponent(&mButtonPanel);
	setStyle("DefaultTabPanelStyle");
}
extTabPanel::~extTabPanel(){}

void extTabPanel::addTab(std::string tab,extPanel* panel){
	if (! containsTab(tab) && panel != NULL){
		mPanelMap[tab] = panel;
		panel->setVisible(false);
		addComponent(panel);
		mButtonPanel.addTabButton(tab);
		if (mPanelMap.size() == 1){
			mButtonPanel.setActive(0);
		}
	}
}

void extTabPanel::update(){
	updateActivePanel();
	extPanel::update();
}

void extTabPanel::updateActivePanel(){
	std::string tab = "";
	
	tri::ui::extCheckBox* comp = mButtonPanel.getSelected();
	if(comp != NULL){
		tab = comp->getText();
	}
	
	tri::TVector p_size = getSize();
	tri::TVector c_size = mButtonPanel.getSize();
	tri::TVector c_pos =  mButtonPanel.getPosition();
		
		
	c_size.y = 24;
	c_size.x = p_size.x - getInsets().getLeft() - getInsets().getRight();
	c_pos.y = p_size.y-c_size.y-getInsets().getTop();
	c_pos.x = getInsets().getLeft();
	mButtonPanel.setSize(c_size);
	mButtonPanel.setPosition(c_pos);
		
		
	
	std::map<std::string,extPanel*>::iterator it;
	for ( it=mPanelMap.begin() ; it != mPanelMap.end(); it++ ){
		(*it).second->setVisible(false);
	}
	if(tab.compare("")!=0){
		
		
		c_pos = mButtonPanel.getPosition();
		c_pos.x = getInsets().getLeft();
		
		tri::TVector size(0,0,0);
		size.x = p_size.x - getInsets().getLeft() - getInsets().getRight();
		size.y = p_size.y-c_size.y-getInsets().getTop()-getInsets().getBottom();
		c_pos.y = getInsets().getBottom();
		
		mPanelMap[tab]->setVisible(true);
		mPanelMap[tab]->setSize(size);
		mPanelMap[tab]->setPosition(c_pos);
	}
}

void extTabPanel::setStyle(std::string style){
	tri::ui::extPanel::setStyle(style);
	mButtonPanel.setStyle(style +std::string(".ButtonPanel"));
}

bool extTabPanel::containsTab(std::string tab){
	return mButtonPanel.containsTab(tab);
}



}
}
